package com.example.administrator.myapplication.set;

import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.administrator.myapplication.R;
import com.example.administrator.myapplication.set.about.AboutMainFragment;
import com.example.administrator.myapplication.tools.TestData;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;

import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.UpdateListener;

/**
 * Created by Administrator on 2018/1/11.
 */

public class UserAdviceFragment extends Fragment{
    private Toolbar toolbar;
    private EditText et_useradvice;
    private Button bt_advicesubmit;
    private String id;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view =inflater.inflate(R.layout.fragment_useradvice,container,false);

        toolbar= (Toolbar) getActivity().findViewById(R.id.toobar_set);
        et_useradvice = (EditText) view.findViewById(R.id.et_advicetext);
        bt_advicesubmit = (Button) view.findViewById(R.id.bt_advice_submit);

        toolbar.setNavigationIcon(R.mipmap.custom_back);
        toolbar.setTitle("用户体验建议");
        toolbar.setTitleMarginStart(35);
        toolbar.setTitleTextColor(Color.parseColor("#ffffff"));
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SetMainFragment fragment =new SetMainFragment();
                FragmentManager fm =getActivity().getFragmentManager();
                FragmentTransaction ft =fm.beginTransaction();
                ft.replace(R.id.fragment_setmain,fragment).commit();
            }
        });

        bt_advicesubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(TextUtils.isEmpty(et_useradvice.getText().toString())){
                    Toast.makeText(getActivity(),"建议没有填写",Toast.LENGTH_SHORT).show();
                }else{
                    String advice=et_useradvice.getText().toString();
                    File file =new File(getActivity().getFilesDir(),"objectid.txt");
                    try {
                        BufferedReader br =new BufferedReader(new InputStreamReader(new FileInputStream(file)));
                        id =br.readLine();
                        br.close();
                        TestData testData =new TestData();
                        testData.setAdvice(advice);
                        testData.update(id, new UpdateListener() {
                            @Override
                            public void done(BmobException e) {
                                Toast.makeText(getActivity(),"提交建议成功",Toast.LENGTH_SHORT).show();

                            }
                        });

                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        return view;
    }
}
